package com.jgdt.falsework.commonssm.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 这里是为了 解决controller 直接返回字符串报错的问题
 * </p>
 *
 * @author chang.zhou
 * @version $
 * @date Created in 2021/6/4 17:55
 */
@Configuration(proxyBeanMethods = false)
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Optional<HttpMessageConverter<?>> converter = converters.stream()
                .filter(x -> x instanceof MappingJackson2HttpMessageConverter).findAny();
        HttpMessageConverter<?> httpMessageConverter = converter.orElseThrow(() -> new RuntimeException("MappingJackson2HttpMessageConverter is not here"));
        converters.add(0, httpMessageConverter);
    }
}
